﻿@model MvcCmsForum.Models.ViewData.ForumModel
@{
    ViewBag.Title = ViewData["PageTitle"];
    Layout = "~/RazorThemes/Default/Views/Shared/_Layout.cshtml";
}
@section HtmlHead{
@if (bool.Parse(ViewData["NoIndex"].ToString()))
{
    <text><meta name="ROBOTS" content="NOINDEX, FOLLOW"></text>
}
<link href="@Url.Content("/Content/start/jquery-ui-1.7.2.custom.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("/RazorThemes/" + Model._CurrentPortal.SkinName + "/css/Forum.css")" rel="stylesheet" type="text/css" />
<style type="text/css">
    label.error
    {
        float: none;
        color: red;
        padding-left: .5em;
        vertical-align: top;
    }
</style>
}
<div>
    <div class="spacer_top">
    </div>
    <div id="thecontent" class="thecontent">
        <h2><a href="/forums/@Model._Forum.Path/">@Model._Forum.Title Forum</a></h2>
        <h3>
            @if (Model._Post.ThreadParentPost != null) { <a href="/forums/forum-@Html.Encode(Model._Post.ThreadParentPost.Forum.Path) /@Html.Encode(Model._Post.ThreadParentPost.Path) ">
                @Html.Encode(Model._Post.ThreadParentPost.Title)</a> }
            <br />
            @Html.Encode(Model._Post.Title)
        </h3>
        <span class="field-validation-error">&nbsp;@Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.")
            @ViewData["message"].ToString() </span>var userVote = int.Parse(ViewData["userVote"].ToString());
        <div id="posts">
            <div id="post-@Model._Post.PostID " class="post">
                <div id="editPostContent">
                    @if ((User.Identity.Name == Model._Post.ThreadUser.UserName) || (User.IsInRole("Editor")
                    || User.IsInRole("Admin") )) { <a href="javascript:void(0);" onclick="EditPost(@Model._Post.PostID , false)">
                        edit</a> }
                </div>
                <div class="vote-button">
                    <a onclick="vote(@Model._Post.PostID , 'up')" class="vote-up@userVote >= 1 ? " selected" : "" "
                        href="javascript:void(0);">Like</a> <strong>@Model._Post.VoteCount</strong>
                    <a class="vote-down@userVote <= -1 ? " selected" : "" "
                                onclick="vote(@Model._Post.PostID , 'down')" href="javascript:void(0);">
                        Dislike</a>
                </div>
                <div id="originalPostContent" class="postbody">
                    @Model._Post.Body.ToString()</div>
                <div id="previewPostContent" class="postbody" style="display: none;">
                </div>
                <div class="posted-last">
                    <span class="posted-at">@Model._Post.AddedDate.ToUtcTimeSinceString() ago</span>
                    <span class="posted-by">by
                        <img src="@Model._Post.AddedByAvatarUrl " />
                        @Model._Post.ThreadUser.UserName.ToProfileLink()</span>
                </div>
            </div>
            <hr style="visibility: hidden;" />
            <h3>
                Replies</h3>
            <div id="forum-post-replies">
                @foreach (var post in Model._Posts) {
                <div class="reply-container">
                    <a id="pst-@post.PostID " href="#"></a>
                    <div id="reply-@post.PostID " class="reply">
                        <div id="editPostContent@post.PostID ">
                        </div>
                        <div class="vote-button">
                            <a onclick="vote(@post.PostID , 'up')" class="vote-up" href="javascript:void(0);">Like</a>
                            <strong>@post.VoteCount</strong> <a class="vote-down" onclick="vote(@post.PostID , 'down')"
                                        href="javascript:void(0);">Dislike</a>
                        </div>
                        <div class="postbody">
                            <div class="reply-header">
                                Reply posted by <span class="name">
                                    <img src="@post.AddedByAvatarUrl " />
                                    @post.ThreadUser.UserName.ToProfileLink()</span> @post.LastPostDate.ToUtcTimeSinceString()
                                ago <a href="/forums/forum-@Html.Encode(post.Forum.Path) /@Html.Encode(post.Path) ">
                                    View Branch And Reply</a> @if (!post.ThreadParentPost.IsAnswered && (User.IsInRole("Moderator")
                                || User.Identity.Name == post.ThreadUser.UserName)) { <a id="mrkanswer-@post.PostID" href="javascript:void(0);" onclick="MarkAnswer(@post.PostID)">
                                    Mark As Answer</a> } if (post.IsAnswer) { <span style="color: Red">Answer</span>
                                }
                            </div>
                            <div class="admin">
                                @if (User.IsInRole("Editor") || User.IsInRole("Admin")) {
                                <div>
                                    <a class="remove" meta:id="@post.PostID " href="#remove">remove</a></div>
                                } if ((User.Identity.Name == post.ThreadUser.UserName) || (User.IsInRole("Editor")
                                || User.IsInRole("Admin") )) {
                                <div>
                                    <a href="javascript:void(0);" onclick="EditPost(@post.PostID , true)">edit</a></div>
                                }
                            </div>
                            <div id="originalPostContent@post.PostID ">
                                <blockquote class="body">
                                    @post.Body
                                </blockquote>
                            </div>
                            <div id="previewPostContent@post.PostID " class="postbody" style="display: none;">
                            </div>
                            <div>
                                @if (post.Replies.Count() > 0) { Replies: } foreach (var reply in post.Replies) {
                                int stubLenght = 100; if (reply.Body.Length < 99) { stubLenght = reply.Body.Length
                                - 1; }
                                <div class="reply-reply">
                                    <blockquote class="body">
                                        @reply.Body.Substring(0, stubLenght)...</blockquote>
                                    by @reply.ReplyUser.UserName.ToProfileLink()
                                </div>
                                }
                            </div>
                        </div>
                    </div>
                </div>
                }
            </div>
            @{var paginationReply = Model._Posts.FirstOrDefault(); }
            @if (paginationReply != null)
            { Html.RenderPartial("ControlsForum/Pager", paginationReply); }
        </div>
        @if (!Model._Post.Closed) {
        <form method="post" action="/forums/posts/@Model._Post.PostID /reply" class="post-reply-create">
        <input type="hidden" id="postId" name="postId" value="@Model._Post.PostID " />
        <input type="hidden" id="title" name="title" value="RE: @Model._Post.Title " />
        <p class="field input">
            <label for="body">
                Enter Your Reply - @Html.ActionLink("Forum Code Is On", "FAQ", "MvcCmsForum", null, new { target = "_blank" })
                - Html is <strong>Off</strong></label><br />
            @Html.TextArea("body", String.Empty, new { @class = "replyTA" }) <span class="input-message">
            </span>
        </p>
        <hr />
        <p>
            <button type="submit" id="post-reply-create-button">
                Add Reply</button></p>
        </form>
        } else {
        <div class="info">
            This post has been closed, and no more replies can be made.</div>
        }
    </div>
</div>
<div id="editPostForm">
    <form action="/forums-editpost" method="post">
    <fieldset>
        <label for="Source">
            Source: @Html.ActionLink("Forum Code Is On", "FAQ", "MvcCmsForum", null, new { target = "_blank" })</label>
        @Html.TextArea("Source", new { @class = "editpost-ta" }) @Html.Hidden("EditedPostID")
        @Html.Hidden("IsReply") @Html.Hidden("PageNo", ViewData["index"])
        <input type="submit" value="Save" />
        <input id="cancelEdit" type="button" value="Cancel" />
    </fieldset>
    </form>
</div>
@section SideBar{
@Html.Partial("ControlsForum/AdminSidebar")
}
@section ScriptContent{
<script src="@Url.Content("/Scripts/jquery-ui-1.7.2.custom.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    LoadRecentPosts();
    var original, preview, cnt;
    var currentPostId;
    var currentIsReply;
    cnt = $('#editPostContent');
    original = $('#originalPostContent');
    preview = $('#previewPostContent');
    function EditPost(postid, isReply) {
        var dlg = $('#editPostForm');
        if (isReply) {
            cnt = $('#editPostContent' + postid);
            original = $('#originalPostContent' + postid);
            preview = $('#previewPostContent' + postid);
            currentIsReply = true;
        }
        else {
            cnt = $('#editPostContent');
            original = $('#originalPostContent');
            preview = $('#previewPostContent');
            currentIsReply = false;
        }
        $('#IsReply').val(currentIsReply);
        currentPostId = postid;
        $('#EditedPostID').val(postid);
        if (!dlg.dialog('isOpen')) {
            $.post('/forums/' + postid + '/source/', function (data) {
                $('#Source').val(data);
                original.hide();
                preview.html(original.html()).show();
                dlg.dialog('open');
            });
        } else {
            dlg.dialog('close');
        }
    }

    var timeout = null;
    $(function () {

        var dlg = $('#editPostForm');
        var cntPos = cnt.position();
        dlg.dialog({ autoOpen: false,
            width: 550,
            position: [cntPos.left - 300 + cnt.outerWidth(), cntPos.top + cnt.outerHeight() + 150],
            show: 'blind',
            hide: 'blind',
            resizable: true,
            beforeclose: function () { original.show(); preview.hide(); }
        });

        $('#Source').keyup(function (e) {
            if (timeout != null) {
                clearTimeout(timeout);
                timeout = null;
            }

            var self = $(this);
            timeout = setTimeout(function () {
                $.post('/forums-editpost/' + currentPostId + '/GetPostPreview',
                           { source: self.val() },
                           function (data) { preview.html(data); });
            }, 250);
        });

        $('#cancelEdit').click(function () {
            dlg.dialog('close');
        });
    });    
    
    
</script>
@if (User.IsInRole("Editor"))
{
    <script src="@Url.Content("/Areas/MvcCmsForum/scripts/manage-forums.js")" type="text/javascript"></script>
}
<script src="@Url.Content("/Areas/MvcCmsForum/scripts/forums.js")" type="text/javascript"></script>
}
